# -*- coding:utf-8 -*-
import base64
import json
import logging
import requests
from Enterprise_wechat_Interface.po import utils


class BasePage:
    env = utils.read_yaml() # 读取环境配置信息
    token=None # 类变量，多个实例，类可以共用这个变量
    def __init__(self):
        if self.token is None:
            # 获取token
            self.token= utils.get_token()

    def send_deciphering(self,data: dict):
        data['url']=str(data['url']).replace('aaa',self.env["aa"][self.env["default"]])
        logging.info(f"请求URL：{data['url']}")
        res=requests.request(data['method'],data['url'],headers=data['headers'])
        # base64解密
        if data['encoding']=='base64':
            return json.loads(base64.b64decode(res.content))
        # 把加密后的响应值发送第三方服务，让第三方解密后返回解密信息
        elif data['encoding']=='private':
            return requests.post("url",data=res.content)

    def send(self,data: dict):
        """
        发起请求
        :param data:
        :return:
        """
        data['url']=str(data['url']).replace('aaa',self.env["apply"][self.env["default"]])
        logging.info(f"请求URL：{data['url']}")
        res=requests.request(data['method'],data['url'],headers=data['headers'],
                             json=data['data'],verify=False)
        logging.info(f"返回{data['method']}响应:{res.json()}")
        # r 有天然优势；状态码， 使用更灵活
        return res




